package com.example.chapter06.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.chapter06.entity.PhoneAndPassword;

import java.util.List;

public class LoginDBHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "PhoneAndPassword.db";

    private static final String TABLE_NAME = "phone_password_table";

    private static final int DB_VERSION = 1;

    private static LoginDBHelper mHelper = null;

    private SQLiteDatabase mWDB = null;

    private SQLiteDatabase mRDB = null;

    private LoginDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static LoginDBHelper getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new LoginDBHelper(context);
        }
        return mHelper;
    }

    public SQLiteDatabase openWriteLink() {
        if (mWDB == null || !mWDB.isOpen()) {
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }

    public SQLiteDatabase openReadLink() {
        if (mRDB == null || !mRDB.isOpen()) {
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    public void closeLink() {
        if (mWDB != null && mWDB.isOpen()) {
            mWDB.close();
            mWDB = null;
        }

        if (mRDB != null && mRDB.isOpen()) {
            mRDB.close();
            mRDB = null;
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( id INTEGER  PRIMARY KEY " +
                "AUTOINCREMENT," +
                "phone VARCHAR NOT NULL UNIQUE,password VARCHAR NOT NULL,remember INTEGER NOT NULL);";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void save(PhoneAndPassword pap){
        try {
            mWDB.beginTransaction();
            delete(pap);
            insert(pap);
            mWDB.setTransactionSuccessful();

        }catch (Exception e){
            Log.d("liu","添加失败");
        }finally {
            mWDB.endTransaction();
        }
    }
    public long delete(PhoneAndPassword pap) {
        return mWDB.delete(TABLE_NAME, "phone=?", new String[]{pap.getPhone()});
    }

    public long insert(PhoneAndPassword pap) {
        ContentValues values = new ContentValues();
        values.put("phone", pap.getPhone());
        values.put("password", pap.getPassword());
        values.put("remember", pap.getRemember());
        return mWDB.insert(TABLE_NAME, null, values);
    }

    public boolean isExistPhone(String phone) {
        @SuppressLint("Recycle") Cursor cursor = mRDB.query(TABLE_NAME, null, "phone=?", new String[]{phone}, null,
                null, null);
        return cursor.getCount() == 1;
    }

    public boolean isExist(PhoneAndPassword pap) {
        @SuppressLint("Recycle") Cursor cursor = mRDB.query(TABLE_NAME, null, "phone=? and password=?", new String[]{pap.getPhone(),
                pap.getPassword()}, null, null, null);
        return cursor.getCount() == 1;
    }

    public long update(PhoneAndPassword pap){
        ContentValues values = new ContentValues();
        values.put("phone", pap.getPhone());
        values.put("password", pap.getPassword());
        values.put("remember", pap.getRemember());
        return mWDB.update(TABLE_NAME,values,"phone=?",new String[]{pap.getPhone()});

    }

    //找到最后一个登录的手机号和密码
    public PhoneAndPassword queryTop(){
        PhoneAndPassword pap = null;
                String sql = "SELECT * FROM "+TABLE_NAME+" WHERE remember = 1 ORDER BY id DESC LIMIT 1";
        @SuppressLint("Recycle")
        Cursor cursor = mRDB.rawQuery(sql, null);
        if(cursor.moveToNext()){
            pap = new PhoneAndPassword();
            pap.setId(cursor.getInt(0));
            pap.setPhone(cursor.getString(1));
            pap.setPassword(cursor.getString(2));
            pap.setRemember(cursor.getInt(3));
        }
        return pap;
    }

    public PhoneAndPassword queryByPhone(String phone){
        PhoneAndPassword pap = null;
        @SuppressLint("Recycle") Cursor cursor = mRDB.query(TABLE_NAME, null, "phone=? and remember = 1", new String[]{phone}, null, null,
                null, null);
        if(cursor.moveToNext()){
            pap = new PhoneAndPassword();
            pap.setId(cursor.getInt(0));
            pap.setPhone(cursor.getString(1));
            pap.setPassword(cursor.getString(2));
            pap.setRemember(cursor.getInt(3));
        }
        return pap;
    }
}
